package org.scboy.rbac.service.imp;

import java.util.Date;
import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacOrgTypeDao;
import org.scboy.rbac.dao.RbacOrgTypeJoinDao;
import org.scboy.rbac.dao.RbacResourceOrgTypeDao;
import org.scboy.rbac.service.OrgTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;

@Service
public class OrgTypeServiceImp implements OrgTypeService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model model) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		model.orderBy("orgTypeSort");
		return dao.page(model);
	}

	@Override
	@Transactional
	public boolean add(Model model) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		
		Model maxModel=dao.maxSortIndex();
		
		Integer orgTypeSort =0; 
		if (maxModel != null && maxModel.getInt("orgTypeSort")!=null) {
			orgTypeSort = maxModel.getInt("orgTypeSort")+1;
		}
		String id = UUID.randomUUID().toString();
		if (model.get("otStatus") == null) {
			model.add("otStatus", 0);
		}
		if (model.get("mainType") == null) {
			model.add("mainType", 0);
		}
		model.add("id", id);
		model.add("orgTypeSort", orgTypeSort);
		model.add("addTime", new Date());
		model.add("editTime", new Date());
		int resultCount = dao.insert(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Model get(String id) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		return dao.get(Model.create().add("id", id));
	}

	@Override
	public boolean update(Model model) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		model.add("editTime", new Date());
		int resultCount = dao.update(model,Model.create().add("id", model.get("id")));
		if (resultCount > 0) {
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public boolean delete(String id) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		RbacOrgTypeJoinDao joinDao=factory.getDao(RbacOrgTypeJoinDao.class);
		RbacResourceOrgTypeDao rot=factory.getDao(RbacResourceOrgTypeDao.class);
		// 删除组织二级组织关联
		joinDao.delete(Model.create().add("jotId", id));
		rot.delete(Model.create().add("orgTypeId", id));
		int resultCount = dao.delete(Model.create().add("id", id));
		if (resultCount > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean checkState(String id) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		Model model = get(id);
		if (model == null) {
			return false;
		}
		model.add("otStatus", !model.getBoolean("otStatus"));
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean top(String id) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		Model model = get(id);
		if (model == null) {
			return false;
		}
		model.add("mainType", !model.getBoolean("mainType"));
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<Model> list(String parentId) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		RbacOrgTypeJoinDao joinDao=factory.getDao(RbacOrgTypeJoinDao.class);
		if (StrUtil.isNotEmpty(parentId)) {
			return joinDao.queryList(Model.create().add("parentId", parentId));
		} else {
			return dao.select(Model.create().add("mainType", true));
		}
	}

	@Override
	public List<Model> listAll(Model model) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		model.add("otStatus", true);
		model.orderBy("orgTypeSort");
		return dao.select(model);
	}

	@Override
	@Transactional
	public boolean sort(String id, int type) {
		RbacOrgTypeDao dao=factory.getDao(RbacOrgTypeDao.class);
		Model model=dao.get(Model.create().add("id", id));
		Model lastModel=null;
		String sortKey="orgTypeSort";
		String idKey="id";
		//降序
		if(type==0) {
			//查询下面一个系统
			lastModel=dao.get(Model.create().GT(sortKey, model.getInt(sortKey)).orderBy(sortKey));
			
		}else {
			//升序
			lastModel=dao.get(Model.create().LT(sortKey, model.getInt(sortKey)).orderBy(sortKey,OrderType.DESC));
			
		}
		if(lastModel!=null) {
			dao.update(Model.create().add(sortKey, lastModel.getInt(sortKey)),Model.create().add(idKey, id));
			dao.update(Model.create().add(sortKey, model.getInt(sortKey)),Model.create().add(idKey, lastModel.getString(idKey)));
		}
		return true;
	}
}
